#!/bin/sh
 
if [ $# -ne 2 ]; then
    HELP
    exit
fi
 
#确定总行数，并且确定参数是否在行数范围
LINECOUNT=`wc -l $1 | awk '{ print $1 }'`
if [ $2 -lt 1 ] || [ $2 -gt $LINECOUNT ]; then
    echo "$2 is not in range of 1-$LINECOUNT"
    HELP
    exit
fi
 
#确定要展现的行数
HALFCOUNT=`expr $LINECOUNT / 2`
SHOW=`expr $2 + 3`
if [ $SHOW -gt $LINECOUNT ]; then
    SHOW=$LINECOUNT
elif [ $SHOW -lt 7 ]; then
    SHOW=7
fi
 
#确定着色行数
#TMP=`expr $LINECOUNT - 3`
if [ $2 -lt 4 ]; then
    COLOR=$2
elif [ $2 -gt `expr $LINECOUNT - 3` ]; then
    COLOR=`expr $LINECOUNT - $2`
    COLOR=`expr 7 - $COLOR`
else
    COLOR=4
fi
 
if [ $SHOW -lt $HALFCOUNT ]; then
    TIMES=0
    #for line in `cat -n $1 | head -n "$SHOW" | tail -n 7`; do
    cat -n $1 | head -n "$SHOW" | tail -n 7 | while read line; do
        TIMES=`expr $TIMES + 1`
        if [ $TIMES -eq $COLOR ]; then
            echo -e " \033[0;31;1m$line\033[0m"
        else
            echo -e " $line"
        fi
    done
else
    COUNT=`expr $LINECOUNT - $SHOW`
    COUNT=`expr $COUNT + 7`
    #for line in `cat -n $1 | tail -n "$COUNT" | head -n 7`; do
    cat -n $1 | tail -n "$COUNT" | head -n 7 | while read line; do
        TIMES=`expr $TIMES + 1`
        if [ $TIMES -eq $COLOR ]; then
            echo -e " \033[0;31;1m$line\033[0m"
        else
            echo -e " $line"
        fi
    done
fi
